import {
  abroadSchoolList,
  userCollectAbroadSchool
} from '../../network/interface.js';

Page({


  data: {
    schools: [],
    pageNumber: 1,
    pageSize: 20,
    onRefresh: true,
    array: ['中国','英国', '澳大利亚', '香港', '新加坡','美国'],
    countryId:'',
    objectArray: [
      {
        id: 2,
        name: '英国'
      },
      {
        id: 3,
        name: '澳大利亚'
      },
      {
        id: 4,
        name: '香港'
      },
      {
        id: 5,
        name: '新加坡'
      },
      {
        id: 6,
        name: '美国'
      }
    ],
   
  },
  //收藏学校
  collectSchool(e) {
    let params = {};
    let that = this;
    let schoolId = e.target.dataset.param;
    params.schoolId = schoolId;
    let userInfo = wx.getStorageSync('userInfo');
    if (userInfo) {
      params.userId = userInfo.userId;
    }
    params.status = 1;
    userCollectAbroadSchool(params, function success(res) {
      if (res['status'] == 200) {
        wx.showToast({
          title: '收藏成功',
        })
      }
      that.onShow();
    }, function fail(res) {

    })
  },

  bindPickerChange: function(e) {
    let that=this;
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      countryId: e.detail.value
    })
    that.onShow();
  },

  clear(){
    let that =this;
    this.setData({
      countryId: ''
    })
    that.onShow();

  },
  //取消收藏
  cancelCollectSchool(e) {
    let params = {};
    let that = this;
    let schoolId = e.target.dataset.param;
    params.schoolId = schoolId;
    let userInfo = wx.getStorageSync('userInfo');
    if (userInfo) {
      params.userId = userInfo.userId;
    }
    params.status = 2;
    userCollectAbroadSchool(params, function success(res) {
      if (res['status'] == 200) {
        wx.showToast({
          title: '取消成功',
        })
      }
      that.onShow();
    }, function fail(res) {

    })
  },


  onShow: function () {
    let that = this;
    let params = {};
    let userInfo = wx.getStorageSync('userInfo');
    if (userInfo) {
      params.userId = userInfo.userId;
      params.countryId=this.data.countryId
      params.pageNumber = this.data.pageNumber;
      params.pageSize = this.data.pageSize;
    }
    abroadSchoolList(params, function success(res) {
      let datas = res['data']['list'];
      if (res["status"] === 200 && datas != null && datas.length > 0) {

        //下拉加载追加

        that.setData({
          schools: res['data']['list']

        })



      } 
    }, function fail(res) {

    })
  },
  //下拉刷新
  onPullDownRefresh() {
    wx.showNavigationBarLoading();
    let that = this;
    that.setData({
      onRefresh: false
    })

    setTimeout(function () {
      that.onShow();
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    }, 500);


  },

  //底部加载
  onReachBottom() {
    var that = this;
    that.setData({
      onRefresh: true
    });
    that.onShow();
  }


})